﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SymulatorEthernetWPF.Shared;
using SymulatorEthernetWPF.SimObjects.Layers.DataLink;
using SymulatorEthernetWPF.SimObjects.Layers;
using SymulatorEthernetWPF.SimObjects.Devices;
using System.Windows.Media.Animation;
using System;
using SymulatorEthernetWPF.SimObjects.Wires;
using System.Windows.Threading;

namespace SymulatorEthernetWPF.SimObjects.Layers.Physical
{
    /// <summary>
    /// Interaction logic for FrameIcon.xaml
    /// </summary>
    public partial class Signal : UserControl
    {
        private readonly Wire _wire;
        private bool _isJam;
        public bool done;
        private Storyboard story = new Storyboard();
        public NetworkInterface source;
        public NetworkInterface destination;
        public bool isJam
        {
            get
            {
                return _isJam;
            }
            set
            {
                _isJam = value;
                if (_isJam == true)
                {
                    BackoffSignal.Visibility = System.Windows.Visibility.Visible;
                    NormalSignal.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    BackoffSignal.Visibility = System.Windows.Visibility.Collapsed;
                    NormalSignal.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        public Ethernet ethernet;

        

        public Point Position
        {
            get
            {
                return (Point)GetValue(PositionProperty);
            }
            set
            {
                SetValue(PositionProperty, value);
            }
        }
        
        public static DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(Point), typeof(Signal), new PropertyMetadata(PositionPropertyChanged));

        public Signal()
        {            
            InitializeComponent();
        }

        private static void PositionPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs prop)
        {
            Signal si = (Signal)dependencyObject;
            Point value = (Point)prop.NewValue;
            Canvas.SetLeft(si, value.X - 10);
            Canvas.SetTop(si, value.Y - 10);
        }
        
        public Signal(WireEnd wireEnd, Ethernet frameData, bool isJam=false)
        {
            InitializeComponent();
            if (wireEnd != null && wireEnd.getOtherEnd() != null)
            {            
                this.source = wireEnd.networkInterface;
                this.destination = wireEnd.getOtherEnd().networkInterface;
                this._wire = wireEnd.wire;
                this.isJam = isJam;    
                this.ethernet = frameData;
            }
            

            this.RegisterName("this", this);
                

            PathGeometry animationPath = new PathGeometry();
            PathFigure pFigure = new PathFigure();       
            PolyLineSegment pPolySegment = new PolyLineSegment();

            

            pFigure.Segments.Add(pPolySegment);
            animationPath.Figures.Add(pFigure);

            
            pFigure.StartPoint = wireEnd.networkInterface.Position;
            if(wireEnd==wireEnd.wire.wireEndA)
            {
                for (int i = 0; i < _wire.WireBenderHandlerList.Count; i++)
                {
                    pPolySegment.Points.Add(_wire.WireBenderHandlerList[i].Position);
                }
            }
            else
            {
                for (int i = _wire.WireBenderHandlerList.Count - 1; i>=0; i--)
                {
                    pPolySegment.Points.Add(_wire.WireBenderHandlerList[i].Position);
                }
            }
            pPolySegment.Points.Add(wireEnd.getOtherEnd().networkInterface.Position);         

            PointAnimationUsingPath centerPointAnimation =   new PointAnimationUsingPath();
            centerPointAnimation.PathGeometry = animationPath;
            if(!isJam)
                centerPointAnimation.Duration = TimeSpan.FromSeconds(100/(double)_wire.connectionType);
            else
                centerPointAnimation.Duration = TimeSpan.FromSeconds(50 / (double)_wire.connectionType);
            Storyboard.SetTarget(centerPointAnimation, this);
            Storyboard.SetTargetProperty(centerPointAnimation, new PropertyPath(Signal.PositionProperty));
            story.Children.Add(centerPointAnimation);
            story.Completed += new EventHandler(story_Completed);
            _wire.signals.Add(this);
        }

        void story_Completed(object sender, EventArgs e)
        {
                   
            StaticVars.MainWindow.customCanvas.Children.Remove(this);
            destination.receiveSignalEnd(this);
            source.wireEnd.signalReceived(this);
            _wire.signals.Remove(this);
        }


        public void pause()
        {
            story.Pause();
        }

        public void resume()
        {
            
            story.Resume();
        }

        public void stop()
        {
            story.Stop();
            destination.inBuffer = null;
            destination.Owner.abortReceiving(this, destination);
            Simulation.simObjects.Remove(this);
            source.wireEnd.signals.Remove(this);
            _wire.signals.Remove(this);
            
        }

        
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            story.Begin();
        }
    }
}
